package com.wicket.okrcalc.biz.service.nb;

import cn.hutool.core.bean.BeanUtil;
import com.wicket.okrcalc.biz.service.dto.common.StandardDataDto;
import com.wicket.okrcalc.common.util.CommonFunctionHelper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import com.wicket.okrcalc.biz.service.dto.req.ObtainStandardDataColumnToMultiRowReqDto;
import com.wicket.okrcalc.biz.service.dto.res.ObtainStandardDataColumnToMultiRowRespDto;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbColumnToRow {
/**
      * code:multiColumnToMultiRow
      * name:M4-获取标准数据列转多行（特殊方法）
      * desc:undefined
      * gen by moon at 11/26/2023, 11:28:49 AM
      * 
      **/
     @Trace(operationName = "M4-获取标准数据列转多行（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ObtainStandardDataColumnToMultiRowRespDto obtainStandardDataColumnToMultiRow(ObtainStandardDataColumnToMultiRowReqDto reqDto) {
         ObtainStandardDataColumnToMultiRowRespDto retData = new ObtainStandardDataColumnToMultiRowRespDto();
         Field[] declaredFields = StandardDataDto.class.getDeclaredFields();
         Map<String, Field> tarGetValueIndex = new HashMap<>();
         for (Field field : declaredFields) {
             tarGetValueIndex.put(field.getName(), field);
         }
         Map<String,StandardDataDto> arData = new HashMap<>();
         for (StandardDataDto oneItem : reqDto.getStandardDataList()) {
             if(!arData.containsKey(oneItem.getEntityId())){
                 StandardDataDto elm = new StandardDataDto();
                 arData.put(oneItem.getEntityId(),elm);
             }
             StandardDataDto elm = arData.get(oneItem.getEntityId());

             String key = CommonFunctionHelper.underlineToHump(oneItem.getDataType());
             Field valueField = tarGetValueIndex.get(key);
             elm.setEntityId(oneItem.getEntityId());
             elm.setFromContentEntityId(oneItem.getFromContentEntityId());



             try {
                 if (valueField != null) {
                     valueField.setAccessible(true);
                     if(Objects.equals(valueField.getType(), String.class)){
                         valueField.set(elm,String.valueOf(oneItem.getDataResult()));
                     }else{
                         valueField.set(elm, oneItem.getDataResult());
                     }

                     Field unitField = tarGetValueIndex.get(key + "Unit");
                     if (unitField != null) {
                         unitField.setAccessible(true);
                         unitField.set(elm, oneItem.getUnit());
                     }
                 }
             } catch (IllegalAccessException e) {
                 e.printStackTrace();
             }
         }
         retData.setStandardDataList(arData.values().stream().map(p-> BeanUtil.toBean(p,StandardDataDto.class)).collect(Collectors.toList()));

         if(retData.getStandardDataList()!=null && retData.getStandardDataList().size()==1){
             StandardDataDto firstData = retData.getStandardDataList().get(0);
             BeanUtil.copyProperties(firstData,retData);
         }
         return retData;
      }
  //手工接入方法
}
